$(document).ready(function(){
    $('.voteinput2').live('change',function(){
        loadvotetype();
    });
});
function loadvotetype() {
    var totalops=0;
    $('.voteinput2').each(function(){
        if ($(this).val()) {
            totalops++;
        }
    });
    if (totalops>=2) {
       $("select[name=ftype] option").each(function(){
            $(this).remove();
        });
        $("select[name=ftype]").append('<option value="1">'+langjs['vote_one']+'</option>');
        for (var i=2; i<=totalops; i++) {
            $("select[name=ftype]").append('<option value="'+i+'">'+langjs['must_select']+i+langjs['vote_unit']+'</option>');
        }
    }
}
function showVoteDIV(vid,obj) {
    var clk=0;
    if ($(obj).parent().parent().find('.morecontent').html()) {
        clk=1;
    }
    $(obj).parent().parent().find('.morecontent').remove();
    var html='<div class="morecontent"><div class="voteshow" onclick="showVotecontent('+vid+',this)"></div></div>';
    $(obj).parent().after(html);
    if (clk==1) {
        $(obj).parent().parent().find('.morecontent').find('.voteshow').click();
    }
}
function showVotecontent(vid,obj,type) {
    if (type!='thin') {
        obj=$(obj).parent();
        type='more';
    }
    var votenickname=$(obj).parent().find('.author ').html();
    if (obj.find('.votecontbox').html()) {
        obj.find('.voteshow').show();
        obj.find('.votecontbox').remove();
    } else {
        obj.find('.voteshow').hide();
        obj.append('<div class="voteloading"><br/><center><img src="'+pubdir+'/images/loading.gif"> '+langjs['vote_loading']+'</center><br/></div>');

        $.post(siteurl+"/Vote/getvote", {vid:vid,type:type,votename:votenickname},
        function(msg){
            obj.find('.voteloading').remove();
            obj.append(msg);
            //bar
            obj.find('.votecontbox > ul >li').each(function(){
                var bwidth=parseInt($(this).find('.optbar > span').css('width'));
                $(this).find('.optbar > span').css('width','2px');
                $(this).find('.optbar > span').animate({ width: bwidth+'px'}, 1000 );
            });
        });
    }
}
function loadvotecomment(vid,page) {
    $('.votecoment').html('<br/><center><img src="'+pubdir+'/images/loading.gif"> '+langjs['vote_comment_loading']+'</center>');
    $.get(siteurl+"/Vote/loadcommont/vid/"+vid+"/page/"+page+"/rand/"+GetRandomNum(1,999999),
    function(msg){
        $('.votecoment').html(msg);
    });
}
function sendvotecoment(vid) {
    tologin();
    if (!vid || vid==undefined || vid==null) {
        ye_msg.open(langjs['data_error'],3,2);
        return;
    }
    var cont=$('#contentbox').val();
    var isret=$('#voterepost:checked').val();
    isret=isret==1?1:0;
    if (!cont) {
        $('#contentbox').bgflash('#ffe9e8', 3);
        return;
    }
    $('#contentbox').val('');
    $.post(siteurl+"/Vote/sendvotecomment", {content:cont,vid:vid,isret:isret},
    function(msg){
        var stdata=jQuery.parseJSON(msg);
        if (stdata.ret=='success') {
            var firstli,firstli2;
            firstli=$(".votecoment li").first();
            firstli.before(stdata.msg);
            firstli2=$(".votecoment li").first();
            firstli2.css("display","none");
            firstli2.animate({height: 'toggle', opacity: 'toggle'}, { duration: "slow" });
            $("#nonemsg").remove();
        } else {
            ye_msg.open(stdata.msg,3,2);
        }
    });
}
function delvotecomment(id,vid,obj) {
    tologin();
    if (!vid || vid==undefined || vid==null) {
        ye_msg.open(langjs['data_error'],3,2);
        return;
    }
    var mymes;
    mymes=confirm(langjs['delvcomment_confirm']);
    if(mymes==true){
        $.post(siteurl+"/Vote/delvotecomment", {id:id,vid:vid},
        function(msg){
            var stdata=jQuery.parseJSON(msg);
            if (stdata.ret=="success") {
                $(obj).parent().parent().parent().parent().animate({opacity: 'toggle'}, "slow");
                ye_msg.open(stdata.msg,1,1);
            } else {
                ye_msg.open(stdata.msg,3,2);
            }
        });
    }
}
function delvote(vid) {
    tologin();
    if (!vid || vid==undefined || vid==null) {
        ye_msg.open(langjs['data_error'],3,2);
        return;
    }
    var mymes;
    mymes=confirm(langjs['delvote_confirm']);
    if(mymes==true){
        $.post(siteurl+"/Vote/delvote", {vid:vid},
        function(msg){
            var stdata=jQuery.parseJSON(msg);
            if (stdata.ret=="success") {
                window.location.href=siteurl+'/Vote';
            } else {
                ye_msg.open(stdata.msg,3,2);
            }
        });
    }
}
function editlasttime(vid) {
    tologin();
    var h,m;
    for (var i=0; i<=23; i++) {
        h+='<option value="'+i+'">'+i+'</option>';
    }
    for (var i=0; i<=59; i++) {
        m+='<option value="'+i+'">'+i+'</option>';
    }
    var html='<div class="votelasttime"><p>'+langjs['vote_lasttime']+'：<input type="input" min="2011-09-15" class="input_text date" id="vtdate">&nbsp;&nbsp;<select id="voteh">'+h+'</select> '+langjs['vote_h']+'&nbsp;<select id="votei">'+m+'</select> '+langjs['vote_i']+'</p><br/><p><center><input type="button" class="button2" value="'+langjs['report_submit']+'" onclick="editlasttimeact(\''+vid+'\')"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class="button3" type="button" value="'+langjs['closewindow']+'" onclick="ye_dialog.close()"/></center></p></div>';
    ye_dialog.openHtml(html,langjs['vote_editlasttime'],'400','135');
    var nowdate=new Date(Date.parse(new Date()) + (86400000 * 7));
    $('#vtdate').val(nowdate.format('yyyy-MM-dd'));
    $('#voteh').val(nowdate.getHours());
    $('#votei').val(nowdate.getMinutes());
    $("#vtdate").dateinput({format:'yyyy-mm-dd'});
}
function editlasttimeact(vid) {
    tologin();
    if (!vid || vid==undefined || vid==null) {
        ye_msg.open(langjs['data_error'],3,2);
        return;
    }
    var vtdate=$('#vtdate').val();
    var voteh=$('#voteh').val();
    var votei=$('#votei').val();

    var lasttime=vtdate+' '+voteh+':'+votei;
    var nowdate=new Date().format('yyyy-MM-dd h:m');
    if (lasttime<=nowdate) {
        ye_msg.open(langjs['lasttimetoless'],3,2);
        return;
    }
    $.post(siteurl+"/Vote/editlasttime",{vid:vid,lasttime:lasttime},
    function(msg){
        var stdata=jQuery.parseJSON(msg);
        if (stdata.ret=="success") {
           ye_msg.open(stdata.msg,1,1);
           ye_dialog.close();
        } else {
            ye_msg.open(stdata.msg,3,2);
        }
    });
}
function addnewoption(vid) {
    tologin();

    var html='<div class="votelasttime"><p style="text-align:left;margin-left:20px">'+langjs['voteoptions20']+'<br/><input id="addnewoption" class="input_text" maxlength="20" style="width:350px;margin-top:10px"></p><br/><p><center><input type="button" class="button2" value="'+langjs['report_submit']+'" onclick="addnewoptionact(\''+vid+'\')"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input class="button3" type="button" value="'+langjs['closewindow']+'" onclick="ye_dialog.close()"/></center></p></div>';
    ye_dialog.openHtml(html,langjs['addnewopts'],'400','165');
}
function addnewoptionact(vid) {
    tologin();
    if (!vid || vid==undefined || vid==null) {
        ye_msg.open(langjs['data_error'],3,2);
        return;
    }
    var addnewoption=$('#addnewoption').val();
    if ($.trim(addnewoption).length>20 || $.trim(addnewoption).length<=0) {
        ye_msg.open(langjs['voteoptions20_2'],3,2);
        return;
    }
    $.post(siteurl+"/Vote/addnewoption",{vid:vid,addnewoption:addnewoption},
    function(msg){
        var stdata=jQuery.parseJSON(msg);
        if (stdata.ret=="success") {
           ye_msg.open(stdata.msg,1,1);
           ye_dialog.close();
        } else {
            ye_msg.open(stdata.msg,3,2);
        }
    });
}
function votectrl(vid,maxnum) {
    $('#vote_'+vid+' > li').find('input:checkbox').change(function(){
        var totalselect=$('#vote_'+vid+' > li').find('input:checked').length;
        if (totalselect>=maxnum) {
            $('#vote_'+vid+' > li').find('input').not("input:checked").attr('disabled','disabled');
        } else {
            $('#vote_'+vid+' > li').find('input').removeAttr('disabled');
        }
    });
}
function voteaction(vid,maxnum,type) {
    tologin();
    if (!vid || vid==undefined || vid==null) {
        ye_msg.open(langjs['data_error'],3,2);
        return;
    }
    var totalselect=$('#vote_'+vid+' > li').find('input:checked').length;
    if (totalselect==0) {
        ye_msg.open(langjs['atleastone'],3,2);
        return;
    }
    if (totalselect>maxnum) {
        ye_msg.open(langjs['mostvote']+maxnum+langjs['vote_unit_2'],3,2);
        return;
    }
    var sendbtn=$('#vote_'+vid).next().find('input:button');
    sendbtn.hide();
    sendbtn.parent().append('<img src="'+pubdir+'/images/loading.gif" style="margin-left:10px" class="voteloadding">');

    var votedata=new Array();
    $('#vote_'+vid+' > li').find('input:checked').each(function(){
        votedata.push($(this).val());
    });
    var isret=$('#vote_'+vid+'_ret:checked').val();
    isret=isret?1:0;
    var isnone=$('#vote_'+vid+'_none:checked').val();
    isnone=isnone?1:0;

    $.post(siteurl+"/Vote/sendvote",{vid:vid,votedata:votedata,isret:isret,isnone:isnone},
    function(msg){
        var stdata=jQuery.parseJSON(msg);
        if (stdata.ret=='success') {
            ye_msg.open(stdata.msg,3,1);
            //reload
            $.post(siteurl+"/Vote/getvote", {vid:vid,type:type},
            function(msg){
                $('#vote_'+vid).parent().parent().append(msg);
                $('#vote_'+vid).parent().remove();
                //bar
                $('#vote_'+vid+' > li').each(function(){
                    var bwidth=parseInt($(this).find('.optbar > span').css('width'));
                    $(this).find('.optbar > span').css('width','2px');
                    $(this).find('.optbar > span').animate({ width: bwidth+'px'}, 1000 );
                });
            });
        } else {
            ye_msg.open(stdata.msg,3,2);
        }
        $('#vote_'+vid).next().find('.voteloadding').remove();
        sendbtn.show();
    });
}
function showvote(){
    var vtbox=$('.votebox').css('display');
    if (vtbox=='none') {
        closemorebox();
        loadvotetype();
        $('.votebox').show();
    } else {
        $('.votebox').hide();
    }
    var nowdate=new Date(Date.parse(new Date()) + (86400000 * 7));
    $('#vtdate').val(nowdate.format('yyyy-MM-dd'));
    $('#voteh').val(nowdate.getHours());
    $('#votei').val(nowdate.getMinutes());
}
function closevote(){
    $('.votebox').hide();
}
function addvoteinfo(){
    var vtbox=$('#voteinfobox').css('display');
    if (vtbox=='none') {
        $('#voteinfobox').show();
    } else {
        $('#voteinfobox').hide();
    }
}
function addvote() {
    var addvts=$('.addv');
    if (addvts.length<20) {
        addvts.last().after('<div class="addv"><em>'+(addvts.length+1)+'.</em><input class="input_text voteinput2" name="vote[]"></div>');
    } else {
        ye_msg.open(langjs['most20'],3,2);
    }
}
function addsendvote(){
    var selectvote=$('input[name=v_item]:checked').attr('link');
    if (selectvote) {
        $('#contentbox').val($('#contentbox').val()+'【'+langjs['isendvote']+'：'+selectvote+'】');
        showvote();
        var len=typenums('#contentbox');
        if (len<0) {
            $(".sendsp").html(langjs['word_exceed']+'<font color="red"><span id="nums">'+(-len)+'</span></font>'+langjs['word']);
        } else {
            $(".sendsp").html(langjs['can_input']+'<span id="nums">'+len+'</span>'+langjs['word']);
        }
        setCaretPosition('contentbox',CheckLen($('#contentbox').val()),CheckLen($('#contentbox').val()));
    }
}
var vcurpage=1;
var pernum=10;
var vdata;
var totalpg;
function voteajax(type) {
    if (type==1) {
        $('#vtab1').show();
        $('#vtab2').hide();
        $('#vote1').attr('class','selected');
        $('#vote2').removeAttr('class');
    } else {
        $('#vtab1').hide();
        $('#vtab2').show();
        $('#vote2').attr('class','selected');
        $('#vote1').removeAttr('class');

        $('#vtab2').html('<br/><center><img src="'+pubdir+'/images/loading.gif"> '+langjs['vote_loading']+'</center><br/>');
        $.get(siteurl+"/Vote/getuservote/"+GetRandomNum(1,999999),
        function(msg){
            var stdata=jQuery.parseJSON(msg);
            if (stdata.ret=='success') {
                var html='<p class="myvotetip">'+langjs['selectedvote']+'</p><ul class="voteitem">';
                vdata=stdata.tip;
                totalpg=Math.ceil(vdata.length/pernum);
                for (var i=(vcurpage-1)*pernum; i<vcurpage*pernum; i++) {
                    if (vdata[i]!=undefined) {
                        html+=vdata[i];
                    }
                }
                html+='</ul>';
                if (totalpg>1) {
                    html+='<div class="myvotepage"><a href="javascript:void(0)" id="myvotepage1" class="noitem" onclick="myvotepage(1);">'+langjs['page_pre']+'</a>&nbsp;&nbsp;&nbsp;<em>'+vcurpage+'/'+totalpg+'</em>&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" id="myvotepage2" onclick="myvotepage(2);">'+langjs['page_next']+'</a></div>';
                }
                html+='<div class="addsendvote"><center><button class="button6" onclick="addsendvote();">'+langjs['addvote']+'</button></center></div>';
                $('#vtab2').html(html);
            } else {
                $('#vtab2').html(stdata.tip);
            }
        });
    }
}
function myvotepage(type) {
    if (type==1) {
        if (vcurpage>1) {
            vcurpage--;
        }
    } else {
        if (vcurpage<totalpg) {
            vcurpage++;
        }
    }
    if (vcurpage==1) {
        $('#myvotepage1').attr('class','noitem');
        $('#myvotepage2').attr('class','');
    }
    if (vcurpage==totalpg) {
        $('#myvotepage1').attr('class','');
        $('#myvotepage2').attr('class','noitem');
    }
    $('.myvotepage').find('em').html(vcurpage+'/'+totalpg);
    var html='';
    for (var i=(vcurpage-1)*pernum; i<vcurpage*pernum; i++) {
        if (vdata[i]!=undefined) {
            html+=vdata[i];
        }
    }
    $('.voteitem').html(html);
}
function sendvote(from){
    tologin();
    var vtitle=$('#votetitle').val();
    var vinfo=$('#voteinfo').val();
    if ($.trim(vtitle).length==0 || $.trim(vtitle).length>25) {
        $('#votetitle').bgflash('#ffe9e8', 3);
        ye_msg.open(langjs['title25'],3,2);
        return;
    }
    var totalvt=0;
    var val;
    var error=0;
    var voptions=new Array();
    $('.addv').each(function(){
        val=$(this).find('input').val();
        if (val) {
            voptions[totalvt]=val;
            totalvt++;
            if ($.trim(val).length>20) {
                $(this).bgflash('#ffe9e8', 3);
                $(this).find('input').bgflash('#ffe9e8', 3);
                error++;
            }
        }
    });
    if (error>0) {
        ye_msg.open(langjs['optsword20'],3,2);
        return;
    }
    if (totalvt<3) {
        $('.addv').bgflash('#ffe9e8', 3);
        $('.addv').find('input').bgflash('#ffe9e8', 3);
        ye_msg.open(langjs['least3opts'],3,2);
        return;
    }
    var lasttime=$('#vtdate').val()+' '+$('#voteh').val()+':'+$('#votei').val();
    var nowdate=new Date().format('yyyy-MM-dd h:m');
    if (lasttime<=nowdate) {
        $('#vtdate').bgflash('#ffe9e8', 3);
        ye_msg.open(langjs['lasttimelesstime'],3,2);
    }
    $('.sendvote').append('<img src="'+pubdir+'/images/spinner.gif" style="margin-left:10px" class="voteloadding">');
    var vtype=$('select[name=ftype]').val();
    $('#sendvotebtn').attr('disabled','disabled');
    $.post(siteurl+"/Vote/sendvoteaction",{vtitle:vtitle,vinfo:vinfo,vtype:vtype,lasttime:lasttime,voptions:voptions},
    function(msg){
        var stdata=jQuery.parseJSON(msg);
        $('.voteloadding').remove();
        if (stdata.ret=='success') {
            refreshvote();
            ye_msg.open(stdata.msg,3,1);
            if (from=='page') {
                setTimeout(function(){
                    window.location.href=siteurl+'/vote/'+stdata.vid;
                },1000);
            } else {
                $('#contentbox').append('【'+langjs['isendvote']+'：'+siteurl+'/vote/'+stdata.vid+'】');
                var len=typenums('#contentbox');
                if (len<0) {
                    $(".sendsp").html(langjs['word_exceed']+'<font color="red"><span id="nums">'+(-len)+'</span></font>'+langjs['word']);
                } else {
                    $(".sendsp").html(langjs['can_input']+'<span id="nums">'+len+'</span>'+langjs['word']);
                }
                setCaretPosition('contentbox',CheckLen($('#contentbox').val()),CheckLen($('#contentbox').val()));
                showvote();
            }
        } else {
            ye_msg.open(stdata.msg,3,2);
        }
        $('#sendvotebtn').removeAttr('disabled');
    });
}
function refreshvote(){
    $('#votetitle').val('');
    $('#voteinfo').val('');
    var totalvt=0;
    $('.addv').each(function(){
        totalvt++;
        $(this).find('input').val('');
        if (totalvt>5) {
            $(this).remove();
        }
    });
    $("select[name=ftype] option").each(function(){
        $(this).remove();
    });
    $("select[name=ftype]").append('<option value="1">'+langjs['vote_one']+'</option>');
    $('#sendvotebtn').removeAttr('disabled');
}